package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.CommonUtil;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.ChargeBillVO;
import com.jishuo.nontax.bfc.service.ChargeBillService;
import org.dom4j.Element;

import java.math.BigDecimal;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description:
 */
public class ChargeBillApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("getChargeBill")) { // 获取缴款书单据
            String billnum = CommonUtil.getNodeText(body.selectSingleNode("//root/body/billnum"));
            if(billnum==null||billnum.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[billnum]");
            }
            ChargeBillService chargeBillService = (ChargeBillService) SpringContextUtil.getBean("chargeBillService");
            return chargeBillService.getChargeBill(billnum);

        } else if (head.equals("setChargeBillDeposit")) { //一般缴款书电子票缴费
            ChargeBillVO chargeBillVO = new ChargeBillVO();
            chargeBillVO.setBillnum(CommonUtil.getNodeText(body.selectSingleNode("//root/body/billnum")));
            chargeBillVO.setEticketnum(CommonUtil.getNodeText(body.selectSingleNode("//root/body/eticketnum")));
            chargeBillVO.setPayer(CommonUtil.getNodeText(body.selectSingleNode("//root/body/payer")));
            chargeBillVO.setFilldate(CommonUtil.getNodeText(body.selectSingleNode("//root/body/filldate")));
            chargeBillVO.setAccountcode(CommonUtil.getNodeText(body.selectSingleNode("//root/body/accountcode")));
            chargeBillVO.setIncomeway(Integer.parseInt(body.selectSingleNode("//root/body/iswaitaffirm").getText()));
            chargeBillVO.setOriginfund(new BigDecimal(body.selectSingleNode("//root/body/originfund").getText()));
            chargeBillVO.setPaybankdt(CommonUtil.getNodeText(body.selectSingleNode("//root/body/paybankdt")));
            chargeBillVO.setBankaffirm(CommonUtil.getNodeText(body.selectSingleNode("//root/body/bankaffirm")));
            chargeBillVO.setBankstation(CommonUtil.getNodeText(body.selectSingleNode("//root/body/bankstation")));
            chargeBillVO.setPaywaycode(CommonUtil.getNodeText(body.selectSingleNode("//root/body/paywaycode")));
            ChargeBillService chargeBillService = (ChargeBillService) SpringContextUtil.getBean("chargeBillService");
            BaseResult ret = chargeBillService.setChargeBillDeposit(chargeBillVO, "00");
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        } else if (head.equals("setChargeBillIsActive")) { //验证手工缴款书是否可用，记录手工缴款书验证信息，并向财政发送信息
            ChargeBillVO chargeBillVO = new ChargeBillVO();
            chargeBillVO.setBillnum(body.selectSingleNode("//root/body/billnum").getText());
            chargeBillVO.setUnitid(Long.valueOf(body.selectSingleNode("//root/body/unitid").getText()));
            chargeBillVO.setCantonid(Long.valueOf(body.selectSingleNode("//root/body/cantonid").getText()));
            ChargeBillService chargeBillService = (ChargeBillService) SpringContextUtil.getBean("chargeBillService");

            return chargeBillService.setChargeBillIsActive(chargeBillVO);

        }else if (head.equals("setChargeBillManual")){ //记录手工缴款书票面和收款信息，并向财政发送信息
            String xml = "";
            if(body.asXML()!=null && body.asXML().trim().length()>7) {
                xml = body.asXML().substring(6, body.asXML().length() - 7);
            }
            ChargeBillService chargeBillService = (ChargeBillService) SpringContextUtil.getBean("chargeBillService");
            BaseResult ret = chargeBillService.setChargeBillManual(xml, "00");
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }

        return null;
    }
}
